home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / abgr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.7 KB  |  171 lines

  1. /* Copyright (c) Silicon Graphics, Inc. 1996 */
  2.  
  3. /*    abgr.c     
  4.  *     Demonstrates the GL_EXT_abgr extension.  This program creates
  5.  *     an image which consists of 4 stripes, each with only one 
  6.  *    component set to 0xff, and the rest set to 0.  This image is 
  7.  *    then used as a texture which is applied first using RGBA format, 
  8.  *    and then again with ABGR_EXT format.
  9.  *
  10.  *    Escape key            - exit program
  11.  */
  12. #include <GL/glut.h>
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. /*  Function Prototypes  */
  18.  
  19. GLvoid initgfx( GLvoid );
  20. GLvoid drawScene( GLvoid );
  21. GLvoid reshape( GLsizei, GLsizei );
  22. GLvoid keyboard( GLubyte, GLint, GLint );
  23.  
  24. GLvoid printHelp( char * );
  25.  
  26. /* Global Definitions */
  27.  
  28. #define KEY_ESC    27    /* ascii value for the escape key */
  29.  
  30. /* Global Variables */
  31. GLubyte     ubImage[65536];
  32. GLubyte        *image;
  33.  
  34. GLvoid
  35. main ( int argc, char *argv[] )
  36. {
  37.     GLsizei width, height;
  38.  
  39.     glutInit( &argc, argv );
  40.  
  41.     width = glutGet(GLUT_SCREEN_WIDTH); 
  42.     height = glutGet(GLUT_SCREEN_HEIGHT);
  43.     glutInitWindowPosition( width/8, height/8); 
  44.     glutInitWindowSize( 400, 400 );
  45.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  46.     glutCreateWindow( argv[0] );
  47.  
  48.     initgfx();
  49.  
  50.     glutKeyboardFunc( keyboard );
  51.     glutReshapeFunc( reshape );
  52.     glutDisplayFunc( drawScene );
  53.  
  54.     printHelp( argv[0] );
  55.  
  56.     glutMainLoop();
  57. }
  58.  
  59. GLvoid
  60. printHelp( char *progname )
  61. {
  62.     fprintf(stdout, "\n%s - demonstrates GL_EXT_abgr extension\n"
  63.         "Escape key            - exit the program\n\n",
  64.         progname);
  65. }
  66.  
  67. GLvoid
  68. initgfx( GLvoid )
  69. {
  70.     int j; 
  71.     glClearColor( 0.5, 0.5, 0.5, 1.0 );
  72.  
  73.     if ( !glutExtensionSupported( "GL_EXT_abgr" ) ) {
  74.         fprintf(stderr, 
  75.             "EXT_abgr not supported in this implementation\n");
  76.     }
  77.  
  78.  
  79.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  80.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  81.  
  82.     /* Create image */
  83.     image = ubImage;
  84.     for (j = 0; j < 32 * 128; j++) { 
  85.         *image++ = 0xff;
  86.         *image++ = 0x00; 
  87.         *image++ = 0x00;
  88.         *image++ = 0x00; 
  89.     }
  90.     for (j = 0; j < 32 * 128; j++) { 
  91.         *image++ = 0x00;
  92.         *image++ = 0xff;
  93.         *image++ = 0x00;
  94.         *image++ = 0x00;
  95.     }
  96.     for (j = 0; j < 32 * 128; j++) {
  97.         *image++ = 0x00;
  98.         *image++ = 0x00;
  99.         *image++ = 0xff;
  100.         *image++ = 0x00;
  101.     }
  102.     for (j = 0; j < 32 * 128; j++) {
  103.         *image++ = 0x00;
  104.         *image++ = 0x00;
  105.         *image++ = 0x00;
  106.         *image++ = 0xff;
  107.     }
  108.     image = ubImage;
  109.  
  110. }
  111.  
  112. GLvoid
  113. reshape( GLsizei width, GLsizei height )
  114. {
  115.     GLdouble            aspect;
  116.  
  117.     glViewport( 0, 0, width, height);
  118.  
  119.     aspect = (GLdouble) width / (GLdouble) height;
  120.  
  121.     glMatrixMode( GL_PROJECTION );
  122.     glLoadIdentity();
  123.     gluPerspective(60.0, aspect, 0.1, 1000.0);
  124.     glMatrixMode( GL_MODELVIEW );
  125.     glLoadIdentity();
  126. }
  127.  
  128. GLvoid 
  129. keyboard( GLubyte key, GLint x, GLint y )
  130. {
  131.     switch (key) {
  132.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  133.         exit(0);
  134.     }
  135. }
  136.  
  137. GLvoid
  138. drawScene( GLvoid )
  139. {
  140.     glClear( GL_COLOR_BUFFER_BIT );
  141.  
  142.     glRasterPos3f(0.2, -0.8, -1.5);
  143.     glDrawPixels(128, 128, GL_RGBA, GL_UNSIGNED_BYTE, image);
  144. #ifdef GL_EXT_abgr
  145.     glRasterPos3f(-0.8, -0.8, -1.5);
  146.     glDrawPixels(128, 128, GL_ABGR_EXT, GL_UNSIGNED_BYTE, image);
  147. #endif
  148.  
  149.     glEnable(GL_TEXTURE_2D);
  150.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGBA,
  151.             GL_UNSIGNED_BYTE, image);
  152.     glBegin(GL_POLYGON);
  153.         glTexCoord2f(1.0, 1.0); glVertex3f(-0.2, 0.8, -100.0);
  154.         glTexCoord2f(0.0, 1.0); glVertex3f(-0.8, 0.8, -2.0);
  155.         glTexCoord2f(0.0, 0.0); glVertex3f(-0.8, 0.2, -2.0);
  156.         glTexCoord2f(1.0, 0.0); glVertex3f(-0.2, 0.2, -100.0);
  157.     glEnd();
  158. #ifdef GL_EXT_abgr
  159.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_ABGR_EXT,
  160.             GL_UNSIGNED_BYTE, image);
  161. #endif
  162.     glBegin(GL_POLYGON);
  163.         glTexCoord2f(1.0, 1.0); glVertex3f(0.8, 0.8, -2.0);
  164.         glTexCoord2f(0.0, 1.0); glVertex3f(0.2, 0.8, -100.0);
  165.         glTexCoord2f(0.0, 0.0); glVertex3f(0.2, 0.2, -100.0);
  166.         glTexCoord2f(1.0, 0.0); glVertex3f(0.8, 0.2, -2.0);
  167.     glEnd();
  168.     glDisable(GL_TEXTURE_2D);
  169.     glutSwapBuffers();
  170. }
  171.